package com.shuimin.as.util;

import pond.common.S;

import java.util.*;

/**
 * 处理微信js调用签名
 * Created by sky on 16/4/1.
 */
public class Sign {


    public static String sign(String app, String key) {
        String timestamp=create_timestamp();
        String nonce=create_nonce_str();
        Map<String,String> map=new HashMap<>();
        map.put("appid",app);
        map.put("key",key);
        map.put("timestamp",timestamp);
        map.put("nonce",nonce);
//        Object[] key_arr = map.keySet().toArray();
//        Arrays.sort(key_arr);
        String[] str=new String[]{app,key,String.valueOf(timestamp),nonce};
        Arrays.sort(str);
        StringBuilder sb = new StringBuilder();
        for(String item:str){
            sb.append(item);
        }
//        for (Object _key : key_arr) {
//            sb.append(_key);
//            sb.append("=");
//            sb.append(map.get(_key));
//            sb.append("&");
//        }

        return "signature="+MD5.GetMD5Code(sb.toString())+"&appid="+app+"&timestamp="+timestamp+"&nonce="+nonce+"&sign_type=md5";
    }


    private static String create_nonce_str() {
        return S.uuid.vid();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
}

